//
//  HJAddMeMethodController.m
//  KaiChat
//
//  Created by lx on 2024/1/16.
//

#import "HJAddMeMethodController.h"
#import "HJSwitchCell.h"
#import "HJSwitchTitleSubTitleCell.h"
#import "HJSectionHeaderView.h"
#import "HJMinePresent.h"
#import "HJUserModel.h"

@interface HJAddMeMethodController ()

@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, assign) BOOL isBanAdd;
@property (nonatomic, strong) HJUserModel *userModel;

@end

@implementation HJAddMeMethodController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBar.title = @"Add Method";
    self.tableViewStyle = UITableViewStyleGrouped;
    [self.tableView registerClass:HJSwitchCell.class forCellReuseIdentifier:@"HJSwitchCell"];
    [self.tableView registerClass:HJSwitchTitleSubTitleCell.class forCellReuseIdentifier:@"HJSwitchTitleSubTitleCell"];
    [self.tableView registerClass:HJSectionHeaderView.class forHeaderFooterViewReuseIdentifier:@"HJSectionHeaderView"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 50;
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(KNavBarHight);
        make.bottom.offset(0);
    }];
    
    dispatch_async_sql_queue(^{
        HJUserModel *userModel = [HJUserModel currentAccountModel];
        self.userModel = userModel;
        self -> _datas = self.datas;
        dispatch_async_main_queue(^{
            self.isBanAdd = userModel.bandAdd;
            [self.tableView reloadData];
        });
    });
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (!_datas) return 0;
    if (_isBanAdd) return 1;
    return _datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_datas[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJCellModel *model = _datas[indexPath.section][indexPath.row];
    TJWeakify(self)
    if ([model.identifier isEqualToString:@"friendRequests"]) {
        HJSwitchTitleSubTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJSwitchTitleSubTitleCell" forIndexPath:indexPath];
        cell.model = model;
        [cell setSwitchValueChange:^(UISwitch * _Nonnull switchView) {
            [weakself setSwitchValueChange:switchView model:model];
        }];
        return cell;
    }
    HJSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJSwitchCell" forIndexPath:indexPath];
    cell.model = model;
    [cell setSwitchValueChange:^(UISwitch * _Nonnull switchView) {
        [weakself setSwitchValueChange:switchView model:model];
    }];
    return cell;
}

- (void)setSwitchValueChange:(UISwitch * _Nonnull)switchView model:(HJCellModel *)model {
    
    HJAddWay way = [model.anyObject integerValue];
    
    [HJHUD showGraceTimeLoadingInView:self.view];
    
    [HJMinePresent privacySettingWithWay:way value:!switchView.isOn complete:^(NSError * _Nullable error) {
        
        if (error) {
            return;
        }
        
        model.isOn = !switchView.isOn;
        [switchView setOn:model.isOn animated:YES];
        
        if (way == HJAddWayBan) {
            self.isBanAdd = model.isOn;
            [self.tableView reloadData];
        }
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    HJCellModel *model = self.datas[indexPath.section][indexPath.row];
    NSString *controllerName = model.controllerName;
    if (isEmptyString(controllerName)) {
        return;
    }
    
    
    
//    privacySettingWithWay
    [self.navigationController pushViewControllerWithName:controllerName];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return  section == 0 ? CGFLOAT_MIN : 20;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HJSectionHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HJSectionHeaderView"];
    view.contentView.backgroundColor = UIColor.tj_F6F6F6Color;
    view.backgroundColor = UIColor.tj_F6F6F6Color;
    view.titleLabel.text = section == 0 ? @"" : @"Add Method";
    view.titleLabel.textColor = UIColor.tj_BEBDCAColor;
    view.titleLabel.font = UIFontMake(12);
    return view;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColor.clearColor;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJCellModel *model = self.datas[indexPath.section][indexPath.row];
    if ([model.identifier isEqualToString:@"friendRequests"]) {
        return model.height;
    }
    return 50;
}

- (NSArray *)datas {
    if (!_datas) {
        _datas = @[
            @[
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"friendRequests";
                    model.text = @"Block All Friend Requests";
                    model.subText = @"Once open, others will not be able to search and add you";
                    model.isOn = self.userModel.bandAdd;
                    CGSize introSize = [model.subText getStringSizeWithWidth:KScreenWidth - 94 font:UIFontMake(14)];
                    model.height = 50 + introSize.height;
                    model.anyObject = @(HJAddWayBan);
                }],
            ],
            @[
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"UID";
                    model.text = @"UID";
                    model.isOn = self.userModel.UIDAdd;
                    model.textFont = UIFontMake(14);
                    model.anyObject = @(HJAddWayUID);
                }],
                [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                    model.identifier = @"email";
                    model.text = @"Email";
                    model.isOn = self.userModel.emailAdd;
                    model.textFont = UIFontMake(14);
                    model.anyObject = @(HJAddWayEmail);
                }]
            ]
        ];
    }
    return _datas;
}

- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end
